package com.gitee.feizns.quickstart.validation.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;

/**
 * 抽象校验器
 * @author feizns
 * @since 2023/2/5
 */
public abstract class AbstractConstraintValidator<A extends Annotation, T> implements ConstraintValidator<A, T> {

    /**
     * 注解
     */
    protected A annotation;

    @Override
    public void initialize(A constraintAnnotation) {
        this.annotation = constraintAnnotation;
    }

    /**
     * 切换消息模板
     * @param context {@link ConstraintValidatorContext}
     * @param template 模板
     */
    protected void withTemplate(ConstraintValidatorContext context, String template) {
        context.disableDefaultConstraintViolation();
        context.buildConstraintViolationWithTemplate(template).addConstraintViolation();
    }

}
